package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.IRawMaterialPriceService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.material.GenerateRawPriceVO;

/**
 * @author 张凯
 * @apiNote 原材耗材价格控制层
 * @since 2022年03月07日 16时18分11秒
 */
@Api(value = "原材耗材价格Web接口", tags = "管理--原材耗材价格操作")
@RequestMapping(value = "/api/rawMaterialPrice")
@RestController
public class RawMaterialPriceController extends ControllerBase {

    @Autowired
    IRawMaterialPriceService service;

    @ApiOperation(value = "查询原材耗材价格", notes = "查询原材耗材价格")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询原材耗材价格", notes = "分页查询原材耗材价格")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "生成原材耗材价格", notes = "生成原材耗材价格")
    @PostMapping("/generate")
    public WebResult generateRawPrice(@RequestBody GenerateRawPriceVO generateVO){
        if(service.generateRawPrice(generateVO)>0){
            return success("ok");
        } else {
            return  fail("生成失败");
        }

    }



    @ApiOperation(value = "保存原材耗材价格", notes = "查询/更新原材耗材价格信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveRawMaterialPrice(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveRawMaterialPrice(submitModel.getData());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }


    @ApiOperation(value = "获取时间分组信息", notes = "获取时间分组信息")
    @GetMapping("/set-time-group")
    public WebResult getSetTimeGroup(){
        return success("ok", service.getSetTimeGroup());
    }

}